/** (c) 2005-2007 Messaging Logic, All Rights Reserved. */
package org.xi8ix.jdbc;

import java.sql.Timestamp;
import java.sql.Types;
import java.util.Date;

/**
 * SQL statements can take any object as a parameter. However, if the parameter is null, the statement
 * cannot guess the correct type, and must be provided with a SQL type code in order to successfully
 * compile the SQL statement. The SimpleParameter class wraps the object and it's type in order to provide
 * unambiguous type information for a SimpleStatement.
 *
 * @author Iain Shigeoka
 * @invariant $none
 */
public class SimpleParameter {

    private Object value;
    private int type;

    /**
     * Create a simple parameter to wrap the given value, with it's corresponding SQL type.
     *
     * @param value the value to wrap
     * @param type the type defined by java.sql.Types
     */
    public SimpleParameter(Object value, int type) {
        this.value = value;
        this.type = type;
    }

    /**
     * Obtain the value wrapped by this simple parameter
     *
     * @return the parameter value
     */
    public Object getValue() {
        return value;
    }

    /**
     * Set the value wrapped by this simple parameter
     *
     * @param value the parameter value
     */
    public void setValue(Object value) {
        this.value = value;
    }

    /**
     * Obtain the SQL type.
     *
     * @return the sql type
     */
    public int getType() {
        return type;
    }

    /**
     * Set the sql type
     *
     * @param type the sql type
     */
    public void setType(int type) {
        this.type = type;
    }

    /**
     * Convenience method to generate a new VARCHAR simple parameter.
     * If the enum is not null, the value will be the result of Enum.ordinal()
     *
     * @param value the value to wrap
     * @return a simple parameter of type VARCHAR
     */
    public static SimpleParameter newBooleanYesNo(Boolean value) {
        String s = null;
        if (value != null) {
            s = value ? "Y" : "N";
        }
        return new SimpleParameter(s, Types.VARCHAR);
    }

    /**
     * Convenience method to generate a new INTEGER simple parameter.
     * If the enum is not null, the value will be the result of Enum.ordinal()
     *
     * @param value the value to wrap
     * @return a simple parameter of type INTEGER
     */
    public static SimpleParameter newBooleanZeroOne(Boolean value) {
        Integer i = null;
        if (value != null) {
            i = value ? 1 : 0;
        }
        return new SimpleParameter(i, Types.INTEGER);
    }

    /**
     * Convenience method to generate a new INTEGER simple parameter.
     * If the enum is not null, the value will be the result of Enum.ordinal()
     *
     * @param e the value to wrap
     * @return a simple parameter of type INTEGER
     */
    public static SimpleParameter newEnumInt(Enum e) {
        Object value = null;
        if (e != null) {
            value = e.ordinal();
        }
        return new SimpleParameter(value, Types.INTEGER);
    }

    /**
     * Convenience method to generate a new VARCHAR simple parameter.
     * If the enum is not null, the value will be the result of Enum.name()
     *
     * @param e the value to wrap
     * @return a simple parameter of type VARCHAR
     */
    public static SimpleParameter newEnumString(Enum e) {
        Object value = null;
        if (e != null) {
            value = e.name();
        }
        return new SimpleParameter(value, Types.VARCHAR);
    }

    /**
     * Convenience method to generate a new VARCHAR simple parameter
     *
     * @param value the value to wrap
     * @return a simple parameter of type VARCHAR
     */
    public static SimpleParameter newString(Object value) {
        if (value != null) {
            value = value.toString();
        }
        return new SimpleParameter(value, Types.VARCHAR);
    }

    /**
     * Convenience method to generate a new INTEGER simple parameter
     *
     * @param value the value to wrap
     * @return a simple parameter of type INTEGER
     */
    public static SimpleParameter newInt(Integer value) {
        return new SimpleParameter(value, Types.INTEGER);
    }

    /**
     * Convenience method to generate a new BIGINT simple parameter
     *
     * @param value the value to wrap
     * @return a simple parameter of type BIGINT
     */
    public static SimpleParameter newLong(Long value) {
        return new SimpleParameter(value, Types.BIGINT);
    }

    /**
     * Convenience method to generate a new FLOAT simple parameter
     *
     * @param value the value to wrap
     * @return a simple parameter of type FLOAT
     */
    public static SimpleParameter newFloat(Float value) {
        return new SimpleParameter(value, Types.FLOAT);
    }

    /**
     * Convenience method to generate a new TIMESTAMP simple parameter.
     * Note that a date can be passed in and if it is not an instance
     * of java.sql.Timestamp it will be wrapped in a new timestamp object.
     *
     * @param value the value to wrap
     * @return a simple parameter of type TIMESTAMP
     */
    public static SimpleParameter newTimestamp(Date value) {
        if (value != null && !(value instanceof Timestamp)) {
            value = new Timestamp(value.getTime());
        }
        return new SimpleParameter(value, Types.TIMESTAMP);
    }

    /**
     * Convenience method to generate a new DATE simple parameter.
     *
     * @param value the value to wrap
     * @return a simple parameter of type DATE
     */
    public static SimpleParameter newDate(Date value) {
        return new SimpleParameter(value, Types.DATE);
    }
}